package com.flux.ui.activity

import java.util.ArrayList
import android.content.Intent
import android.os.AsyncTask
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v4.app.FragmentManager
import android.support.v4.app.FragmentPagerAdapter
import android.support.v7.app.AlertDialog
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.flux.R
import com.flux.Consts
import com.flux.ui.widget.photoview.PhotoView
import com.flux.ui.widget.photoview.PhotoViewAttacher
import com.flux.utils.BaseFileUtil
import com.flux.utils.GlideUtil
import com.flux.utils.ToastUtil
import kotlinx.android.synthetic.main.activity_image_preview.*

/**
 * 查看图片详情页
 */
class FImagePreviewActivity : FBaseActivity() {
    private lateinit var paths: ArrayList<String>

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_image_preview)
        if (null != intent && intent.hasExtra(Consts.Keys.image_preview_activity_source)) initView()
    }

    private fun initView() {
        paths = intent.getStringArrayListExtra(Consts.Keys.image_preview_activity_source)
        val selected = intent.getIntExtra(Consts.Keys.dp_image_preview_activity_selected_pos, 0)

        if (paths.size == 1) {
            indicator.visibility = View.GONE
        } else {
            indicator.visibility = View.VISIBLE
        }
        val adapter = MyAdapter(supportFragmentManager, this, paths)
        viewPager.adapter = adapter
        indicator.setViewPager(viewPager)
        viewPager.currentItem = selected
    }

    fun save(path: String) {
        showToast("正在保存....请稍等")
        val a = SaveImageAs { uri ->
            sendBroadcast(Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri))
            ToastUtil.showToast(this, "图片已保存到图库")
        }
        a.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, path)
    }

    private class MyAdapter internal constructor(fm: FragmentManager, private val activity: FImagePreviewActivity, private val data: ArrayList<String>) : FragmentPagerAdapter(fm) {

        override fun getCount(): Int {
            return data.size
        }

        override fun getItem(position: Int): Fragment {
            val fragment = MyFragment()
            fragment.setActivity(activity)
            fragment.setPath(data[position])
            return fragment
        }
    }

    class MyFragment : Fragment() {
        private lateinit var activity: FImagePreviewActivity
        private lateinit var path: String

        override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
            val photoView = PhotoView(activity)
            GlideUtil.load(activity, path, photoView)
            photoView.onPhotoTapListener = object : PhotoViewAttacher.OnPhotoTapListener {
                override fun onPhotoTap(view: View, x: Float, y: Float) {
                    activity.finish()
                }

                override fun onOutsidePhotoTap() {
                    activity.finish()
                }
            }
            photoView.setOnLongClickListener { _ ->
                if (path.startsWith(BaseFileUtil.FILE_PREFIX)) false
                else {
                    context?.let { context ->
                        AlertDialog.Builder(context)
                                .setItems(arrayOf("保存图片")) { dialog, _ ->
                                    dialog.dismiss()
                                    activity.save(path)
                                }.setCancelable(true)
                                .show()
                                .setCanceledOnTouchOutside(true)
                    }
                    true
                }
            }
            return photoView
        }

        fun setActivity(activity: FImagePreviewActivity) {
            this.activity = activity
        }

        fun setPath(path: String) {
            this.path = path
        }
    }
}